const router = require('koa-router')()
const fs = require('fs');
const path = require('path');

//查询全部攻略
router.get('/getessay', async (ctx, next) => {
    let sql = "select * from essay order by id DESC";
    let result = await ctx.db.EXCUTE(sql);
    for (let i in result) {
        //发布时间格式化
        let publishObj = new Date(result[i].publish);
        result[i].publish = publishObj.getFullYear() + '-' + (publishObj.getMonth() + 1) + '-' + publishObj.getDate() + ' '
            + publishObj.getHours() + ':' + publishObj.getMinutes() + ':' + publishObj.getSeconds();
        // 修改时间格式化
        if (result[i].change) {
            let dateObj = new Date(result[i].change);
            result[i].change = dateObj.getFullYear() + '-' + (dateObj.getMonth() + 1) + '-' + dateObj.getDate() + ' '
                + dateObj.getHours() + ':' + dateObj.getMinutes() + ':' + dateObj.getSeconds();
        }
        //添加img属性：此id包含的所有图片
        let imagesql = `select * from essayimage where id=?`;
        let dataimg = await ctx.db.EXCUTE(imagesql, [result[i].id]);
        result[i].img = dataimg
    }
    for (let i in result) {
        let arr = result[i].img
        for (let k in arr) {
            let header = arr[k].img.split('/')[0];
            if (header == '..') {
                let arr1 = arr[k].img.split('/');
                let arr2 = arr1.splice(2, 4)
                arr2 = arr2.join('/')
                //绝对地址
                let filePath = path.join(__dirname, '../../../小程序/', arr2)
                console.log(filePath)
                let fileObj = fs.readFileSync(filePath);
                let file = fileObj.toString('base64');
                let imgUrl = `data:image/png;base64,${file}`
                arr[k].img = imgUrl;
            }
        }
    }
    ctx.body = { data: result, status: "获取信息成功" }
})

//搜索某个攻略
router.post('/selessay', async (ctx, next) => {
    let user = ctx.request.body.user;
    let sql = `select * from essay where user like'%${user}%' or title like '%${user}%' order by id DESC`;
    let result = await ctx.db.EXCUTE(sql, [user]);
    if (result.length != 0) {
        for (let i in result) {
            //发布时间格式化
            let publishObj = new Date(result[i].publish);
            result[i].publish = publishObj.getFullYear() + '-' + (publishObj.getMonth() + 1) + '-' + publishObj.getDate() + ' '
                + publishObj.getHours() + ':' + publishObj.getMinutes() + ':' + publishObj.getSeconds();
            // 修改时间格式化
            if (result[i].change) {
                let dateObj = new Date(result[i].change);
                result[i].change = dateObj.getFullYear() + '-' + (dateObj.getMonth() + 1) + '-' + dateObj.getDate() + ' '
                    + dateObj.getHours() + ':' + dateObj.getMinutes() + ':' + dateObj.getSeconds();
            }
            //添加img属性：此id包含的所有图片
            let imagesql = `select * from essayimage where id=?`;
            let dataimg = await ctx.db.EXCUTE(imagesql, [result[i].id]);
            result[i].img = dataimg
        }
        for (let i in result) {
            let arr = result[i].img
            for (let k in arr) {
                let header = arr[k].img.split('/')[0];
                if (header == '..') {
                    let arr1 = arr[k].img.split('/');
                    let arr2 = arr1.splice(2, 4)
                    arr2 = arr2.join('/')
                    //绝对地址
                    let filePath = path.join(__dirname, '../../../小程序/', arr2)
                    console.log(filePath)
                    let fileObj = fs.readFileSync(filePath);
                    let file = fileObj.toString('base64');
                    let imgUrl = `data:image/png;base64,${file}`
                    arr[k].img = imgUrl;
                }
            }
        }
        ctx.body = { code: 200, status: "搜索信息成功", data: result }
    } else {
        ctx.body = { code: 400, status: "输入信息错误,无数据", data: [] }
    }

})

//删除攻略
router.post('/delessay', async (ctx, next) => {
    let id = ctx.request.body.id;
    let delsql = 'delete from essay where id=?';
    let result = await ctx.db.EXCUTE(delsql, [id]);
    ctx.body = { "code": 200, "msg": "ok", status: "删除成功" };
})

//审核通过
router.post('/sucessay', async (ctx, next) => {
    let id = ctx.request.body.id;
    let examine = ctx.request.body.examine;
    let sql = 'update essay set examine=? where id=?';
    let result = await ctx.db.EXCUTE(sql, [examine, id]);
    ctx.body = { "code": 200, "msg": "ok", status: "审核通过" };
})

//审核失败
router.post('/passessay', async (ctx, next) => {
    let id = ctx.request.body.id;
    let examine = ctx.request.body.examine;
    let sql = 'update essay set examine=? where id=?';
    let result = await ctx.db.EXCUTE(sql, [examine, id]);
    ctx.body = { "code": 200, "msg": "ok", status: "审核不通过" };
})

module.exports = router